/**
 * 项目服务
 */
 import { Op } from 'sequelize';
 import { v4 as uuidv4 } from 'uuid';
 
 import { Project } from 'src/models/project';
 import SequelizeHelper from 'src/sequelize/index';
 import { PageInfo } from 'src/typings/index.d';
 
 export default class ProjectService {
   constructor() {
     SequelizeHelper.getInstance();
   }
 
   /**
    * 获取列表
    * @param {}
    * @returns {Project[]} 项目列表
    */
   getList = (query?: PageInfo & any) => {
     const where = {};
     const pageQuery = {
       curr: +query.curr,
       size: +query.size,
     };
     if (query.name) {
       Object.assign(where, {
         name: {
           [Op.like]: `%${query.name}%`,
         },
       });
     }
     if (query.cateId) {
       Object.assign(where, {
        cateId: {
           [Op.eq]: query.cateId,
         },
       });
     }
     if (query.desc) {
       Object.assign(where, {
         desc: {
           [Op.like]: `%${query.desc}%`,
         },
       });
     }
     if (query.status) {
       Object.assign(where, {
         status: {
           [Op.eq]: query.status,
         },
       });
     }
     if (query.basis) {
       Object.assign(where, {
        basis: {
           [Op.eq]: query.basis,
         },
       });
     }
     return Project.findAndCountAllPage({
       where,
       ...pageQuery,
     });
   };
 
   /**
    * 新建项目
    * @param {Project} project 项目参数
    * @returns {Project} 新建项目
    */
   create = (project?: Partial<Project>) => {
     const newProject = {
       ...project,
       id: uuidv4().substring(0, 8),
     };
     return Project.create(newProject as Project);
   };
 
   /**
    * 更新项目
    * @param {Project} project 项目参数
    * @returns {Project} 更新项目
    */
   update = (id: string, project?: Partial<Project>) => {
     console.log("project", project)
     Project.update(
       {
         ...project,
       },
       {
         where: {
           id: id
         },
       },
     );
   };
 
   /**
    * 获取详情
    * @param {string} id 项目id
    * @returns {Project} 项目数据
    */
   detail = (id: string) => Project.findByPk(id);
 
   /**
    * 删除项目
    * @param {String} id 项目id
    * @return {Project} 项目数据
    */
   delete = (id: string) => {
     Project.update(
       {
         status: 0,
       },
       {
         where: {
           id: id
         },
       },
     );
   };
 }
 